package com.walkline.screen;

import java.util.Hashtable;

import localization.WeiboSDKResource;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.RealtimeClockListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

import com.walkline.util.Enumerations.MenuType;
import com.walkline.util.Enumerations.RefreshAction;
import com.walkline.util.Enumerations.RefreshRemindDelay;
import com.walkline.util.Enumerations.StatusCount;
import com.walkline.util.Function;
import com.walkline.util.container.ForegroundManager;
import com.walkline.util.container.MainMenuListStyleButtonSet;
import com.walkline.util.ui.MainMenuListStyleButtonField;
import com.walkline.weibo.BasicAsyncCallback;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Remind;

public class MessageBoxMainScreen extends MainScreen implements WeiboSDKResource, RealtimeClockListener
{
	private static ResourceBundle _bundle = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private WeiboSDK _weibo;
	//private Remind _reminds;

	private ForegroundManager _foreground=new ForegroundManager(0);
	private MainMenuListStyleButtonSet _listSet = new MainMenuListStyleButtonSet();
	private MainMenuListStyleButtonField _itemMentionStatus;
	private MainMenuListStyleButtonField _itemMentionComment;
	private MainMenuListStyleButtonField _itemCommentToMe;
	private MainMenuListStyleButtonField _itemCommentByMe;
	//private MainMenuListStyleButtonField _itemPrivateMsg;

	private String _page_count = "20";
	private String _current_page = "1";
	private String _max_id = "0";
	private int _refresh_remind_delay = 3;
	private static int _clockUpdateCount = 0;

	public MessageBoxMainScreen(WeiboSDK weibo)
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | NO_SYSTEM_MENU_ITEMS);

		_weibo = weibo;
		_page_count = StatusCount.choicesStatusCount[weibo.getAppConfig().getStatusCount()];
		_refresh_remind_delay = RefreshRemindDelay.choicesRefreshDelayValue[weibo.getAppConfig().getRemindDelay()];

		HorizontalFieldManager hfm = new HorizontalFieldManager(FIELD_HCENTER);
		hfm.add(new LabelField("消息"));
		setTitle(hfm);

		createUI();

		add(_foreground);

		UiApplication.getUiApplication().addRealtimeClockListener(this);

		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run() {updateReminds();}
		});
	}

	private void createUI()
	{
		//String title = "@我的微博";
		_itemMentionStatus = new MainMenuListStyleButtonField(MenuType.STATUSMENTIONS);
		_itemMentionStatus.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context)
			{
				Hashtable params = new Hashtable();

				params.put("title", _itemMentionStatus.getMenuTitle());
				params.put("count", _page_count);
				params.put("page", _current_page);
				params.put("max_id", _max_id);

				UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.STATUSMENTIONS));
			}
		});
		_listSet.add(_itemMentionStatus);

		//title = "@我的评论";
		_itemMentionComment = new MainMenuListStyleButtonField(MenuType.COMMENTMENTIONS);
		_itemMentionComment.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context)
			{
				Hashtable params = new Hashtable();

				params.put("title", _itemMentionComment.getMenuTitle());
				params.put("count", _page_count);
				params.put("page", _current_page);
				params.put("max_id", _max_id);

				UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.COMMENTMENTIONS));
			}
		});
		_listSet.add(_itemMentionComment);

		//title = "我收到的评论";
		_itemCommentToMe = new MainMenuListStyleButtonField(MenuType.COMMENTTOME);
		_itemCommentToMe.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context)
			{
				Hashtable params = new Hashtable();

				params.put("title", _itemCommentToMe.getMenuTitle());
				params.put("count", _page_count);
				params.put("page", _current_page);
				params.put("max_id", _max_id);

				UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.COMMENTTOME));
			}
		});
		_listSet.add(_itemCommentToMe);

		//title = "我发出的评论";
		_itemCommentByMe = new MainMenuListStyleButtonField(MenuType.COMMENTBYME);
		_itemCommentByMe.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context)
			{
				Hashtable params = new Hashtable();

				params.put("title", _itemCommentByMe.getMenuTitle());
				params.put("count", _page_count);
				params.put("page", _current_page);
				params.put("max_id", _max_id);

				UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.COMMENTBYME));
			}
		});
		_listSet.add(_itemCommentByMe);

		/*
		//title = "私信";
		_itemPrivateMsg = new MainMenuListStyleButtonField(MenuType.PRIVATEMESSAGE);
		_itemPrivateMsg.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context) {}
		});
		_itemPrivateMsg.setEnabled(false);
		_listSet.add(_itemPrivateMsg);
		*/

		_foreground.add(_listSet);
	}

	/**
	 * 刷新消息提醒数量
	 */
	private void refreshRemind()
	{
		RefreshContentsScreen popupScreen = new RefreshContentsScreen(_weibo, new Hashtable(), RefreshAction.REFRESHREMIND);
		UiApplication.getUiApplication().pushModalScreen(popupScreen);

		Remind reminds = popupScreen.getReminds();

		if (popupScreen != null) {popupScreen = null;}
		if (reminds != null)
		{
			_weibo.setReminds(reminds);

			updateReminds();
		}
	}

	/**
	 * 更新消息提醒数量
	 */
	private void updateReminds()
	{
		Remind reminds = _weibo.getReminds();

		_itemMentionStatus.updateUnreadCount(reminds.getMentionStatus());
		_itemMentionComment.updateUnreadCount(reminds.getMentionCmt());
		_itemCommentToMe.updateUnreadCount(reminds.getCmt());
		//_itemPrivateMsg.updateUnreadCount(reminds.getDm());
	}

	class GetRemindCallback extends BasicAsyncCallback
	{
		public void onComplete(final Remind value, Object state)
		{
			if (value != null)
			{
				_weibo.setReminds(value);

				UiApplication.getUiApplication().invokeLater(new Runnable()
				{
					public void run() {updateReminds();}
				});
			}
		}

		public void onException(Exception e, Object state) {Function.errorDialog(e.toString());}
	}

	protected void onExposed()
	{
		updateReminds();

		super.onExposed();
	};

	MenuItem menuRefreshMessage = new MenuItem(_bundle, MENU_REFRESH_MESSAGE, 100, 10)
    {
    	public void run() {refreshRemind();}
    };

    MenuItem menuExit = new MenuItem(_bundle, MENU_EXIT, 100, 80)
    {
    	public void run() {Function.showExitDialog();}
    };

	protected void makeMenu(Menu menu, int instance)
	{
		menu.add(menuRefreshMessage);
		menu.addSeparator();
		menu.add(menuExit);

		super.makeMenu(menu, instance);
	};

	protected boolean keyChar(char character, int status, int time)
    {
    	switch (character)
    	{
			case Characters.BACKSPACE:
				return true;
			case Characters.LATIN_CAPITAL_LETTER_R:
			case Characters.LATIN_SMALL_LETTER_R:
				refreshRemind();
				return true;
			case Characters.LATIN_CAPITAL_LETTER_T:
			case Characters.LATIN_SMALL_LETTER_T:
				//if (_listSet.getFieldCount() > 0) {_listSet.getField(0).setFocus();}
				return true;
			case Characters.LATIN_CAPITAL_LETTER_B:
			case Characters.LATIN_SMALL_LETTER_B:
				//if (_listSet.getFieldCount() > 0) {_listSet.getField(_listSet.getFieldCount()-1).setFocus();}
				return true;
			case Characters.LATIN_CAPITAL_LETTER_E:
			case Characters.LATIN_SMALL_LETTER_E:
				return true;
			case Characters.ESCAPE:
				break;
			case Characters.LATIN_CAPITAL_LETTER_A:
			case Characters.LATIN_SMALL_LETTER_A:
				return true;
			case Characters.LATIN_CAPITAL_LETTER_Q:
			case Characters.LATIN_SMALL_LETTER_Q:
				Function.showExitDialog();
				return true;
		}

    	return super.keyChar(character, status, time);
    }

	public void clockUpdated()
	{
		_clockUpdateCount += 1;

		if (_clockUpdateCount == _refresh_remind_delay);
		{
			_clockUpdateCount = 0;
			_weibo.getRemind(new GetRemindCallback(), null);	
		}
	}
}